package com.yunji.web.storage;

import com.github.pagehelper.page.PageMethod;
import com.yunji.aggregate.model.dto.base.BelongStockParam;
import com.yunji.aggregate.model.dto.stock.StockParam;
import com.yunji.aggregate.model.vo.base.BelongStockStatisticsVO;
import com.yunji.aggregate.model.vo.stock.StockStatisticsVO;
import com.yunji.aggregate.service.StockAggregateService;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.enums.common.CacheCodeExtend;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.storage.business.StockBusiness;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.web.storage
 * @Title: StockController
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-15 15:55:03      V1.0        HuaAo       新建类
 */
@Api(tags = "管理端-库存模块")
@RestController
@RequestMapping("storage/stock")
public class StockController extends BaseController {

    @Resource
    StockBusiness business;

    @Resource
    StockAggregateService aggregateService;

    /**
     * @param param
     * @Title export
     * @Desc 导出库存列表
     * @Date 2024-10-10 15:00:55.526
     */
    @ApiOperation("导出商品库存列表")
    @PostMapping("/export")
//    @PreAuthorize("@ss.hasPermi('inventory:management:export')")
    public void export(StockParam param) {
        List<StockStatisticsVO> list = aggregateService.searchStockList(param);
        ExcelUtil<StockStatisticsVO> util = new ExcelUtil<>(StockStatisticsVO.class);
        util.exportExcel(response, list, "库存列表数据");
    }

    /**
     * 查询商品库存列表
     */
    @ApiOperation("查询商品库存列表")
    @PostMapping("/list")
    public Response<List<StockStatisticsVO>> list(@RequestBody @Valid StockParam param) {
        PageMethod.startPage(param.getPageNum(), param.getPageSize());
        List<StockStatisticsVO> list = aggregateService.searchStockList(param);
        return ResponseHelper.successWithPage(list);
    }

    /**
     * @param stockId   库存ID
     * @param warmValue 预警值
     * @Title setWarmValue
     * @Desc 设置预警值
     * @Date 2024-08-31 17:02:47.297
     * @Return ResponseString>
     */
    @ApiOperation("设置预警值")
    @PutMapping("/setWarmValue")
    public Response<String> setWarmValue(@RequestParam("stockId") Long stockId, @RequestParam("warmValue") BigDecimal warmValue) {
        business.setWarmValue(stockId, warmValue);
        return ResponseHelper.success("设置成功");
    }

    /**
     * @param param
     * @Title belongStockList
     * @Desc 所属库存列表
     * @Date 2024-10-23 15:14:48.681
     * @Return ResponseList < BelongStockVO>>
     */
    @ApiOperation("所属库存列表")
    @PostMapping("/belongStockList")
    public Response<List<BelongStockStatisticsVO>> belongStockList(@RequestBody BelongStockParam param) {
        List<BelongStockStatisticsVO> list = aggregateService.belongStockList(param);
        return ResponseHelper.successWithPage(list);
    }

    /**
     * @param param
     * @Title export
     * @Desc 导出所属库存列表
     * @Date 2024-11-12 17:30:40.607
     */
    @ApiOperation("导出所属库存列表")
    @PostMapping("/belongStockExport")
    public void export(BelongStockParam param) {
        List<BelongStockStatisticsVO> list = aggregateService.belongStockList(param);
        ExcelUtil<BelongStockStatisticsVO> util = new ExcelUtil<>(BelongStockStatisticsVO.class);
        util.exportExcel(response, list, "所属库存列表数据");
    }

    @ApiOperation("库存开关操作")
    @PutMapping("/operateStock")
    public Response<String> operateStock(@RequestParam("belongId") Long belongId, @RequestParam("stockStatus") Integer stockStatus) {
        business.operateStock(belongId, stockStatus);
        return ResponseHelper.success();
    }

    @ApiOperation("删除库存")
    @DeleteMapping("/{ids}")
    public Response<?> remove(@PathVariable Long... ids) {
        business.deleteEmptyStock(ids);
        return ResponseHelper.success();
    }

    /**
     * @param operateType 0-开启 1-关闭
     * @Title operateSwitch
     * @Desc 库存开关
     * @Date 2024-09-19 16:23:37.239
     * @Return ResponseString>
     */
    @ApiOperation("库存开关")
    @PutMapping("/operateSwitch")
    public Response<String> operateSwitch(@RequestParam("operateType") Integer operateType) {
        boolean status = Objects.equals(operateType, LogicCode.NO) ? Boolean.TRUE : Boolean.FALSE;
        redisCache.set(CacheCodeExtend.PARAM_001001.getParam(), status);
        return ResponseHelper.success("操作成功");
    }

    /**
     * @Title getSwitch
     * @Desc 获取库存开关
     * @Date 2024-09-19 19:49:55.438
     * @Return ResponseBoolean>
     */
    @ApiOperation("获取开关")
    @GetMapping("/getSwitch")
    public Response<Boolean> getSwitch() {
        Boolean status = redisCache.get(CacheCodeExtend.PARAM_001001.getParam());
        return ResponseHelper.success(BeanUtils.isNotNull(status) ? status : Boolean.FALSE);
    }


    /*-----------------------------------------------搜索栏------------------------------------------------*/

    @ApiOperation("商品名称匹配搜索")
    @PostMapping("/searchProductName")
    public Response<List<String>> searchProductName(@RequestBody StockParam param) {
        return ResponseHelper.successWithPage(aggregateService.searchProductName(param));
    }

}